package edu.lingnan.librarymanage.service.impl;

import edu.lingnan.librarymanage.core.bean.Admin;
import edu.lingnan.librarymanage.core.bean.User;
import edu.lingnan.librarymanage.core.constant.LibraryManageConstant;
import edu.lingnan.librarymanage.core.dto.LoginUserDTO;
import edu.lingnan.librarymanage.core.dto.RegisterDTO;
import edu.lingnan.librarymanage.core.exception.CaptchaException;
import edu.lingnan.librarymanage.core.exception.UserException;
import edu.lingnan.librarymanage.core.vo.LoginUserVO;
import edu.lingnan.librarymanage.repository.AdminRepository;
import edu.lingnan.librarymanage.repository.UserRepository;
import edu.lingnan.librarymanage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author cql
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private AdminRepository adminRepository;

    @Override
    public LoginUserVO login(LoginUserDTO loginUser) {
        /**
         * 先判断是否过期
         */
        String realCode = loginUser.getRealCode();
        Long expires = loginUser.getExpires();
        if (realCode == null || expires == null) {
            throw new CaptchaException("验证码过期");
        }
        long now = System.currentTimeMillis();
        // 毫秒
        long time = now - expires;
        // 大于5分钟，说明过期了
        if (time / LibraryManageConstant.MINUTE > LibraryManageConstant.CAPTCHA_EXPIRATION) {
            throw new CaptchaException("验证码过期");
        }
        /**
         * 验证码不一致
         */
        if (!realCode.equalsIgnoreCase(loginUser.getCode())) {
            throw new CaptchaException("验证码错误");
        }
        /**
         * 校验用户名和密码(密码没加密)
         */
        if (loginUser.isAdmin()) {
            Admin admin = adminRepository.findByAccountAndPassword(loginUser.getAccount(), loginUser.getPassword());
            if (admin == null) {
                throw new UserException("账号或密码错误");
            }
            LoginUserVO loginUserVo = new LoginUserVO();
            loginUserVo.setUserId(admin.getAdminId());
            loginUserVo.setAccount(admin.getAccount());
            loginUserVo.setAdmin(true);
            return loginUserVo;
        } else {
            User user = userRepository.findByAccountAndPassword(loginUser.getAccount(), loginUser.getPassword());
            if (user == null) {
                throw new UserException("账号或密码错误");
            }
            LoginUserVO loginUserVo = new LoginUserVO();
            loginUserVo.setUserId(user.getUserId());
            loginUserVo.setAccount(user.getAccount());
            loginUserVo.setAdmin(false);
            return loginUserVo;
        }
    }

    @Override
    public LoginUserVO register(RegisterDTO registerDto) {
        /**
         * 校验邮箱是否重复
         * 校验用户名是否重复
         */
        User user = userRepository.findByAccountOrEmail(registerDto.getAccount(), registerDto.getEmail());
        if (user != null) {
            if (user.getAccount().equals(registerDto.getAccount())) {
                throw new UserException("此账号已被注册!");
            } else {
                throw new UserException("此邮箱已被使用!");
            }
        }

        user = new User();
        user.setAccount(registerDto.getAccount());
        user.setPassword(registerDto.getPassword());
        user.setEmail(registerDto.getEmail());
        user.setGender(registerDto.getGender());

        user = userRepository.save(user);
        if (user != null) {
            LoginUserVO loginUserVo = new LoginUserVO();
            loginUserVo.setUserId(user.getUserId());
            loginUserVo.setAdmin(false);
            loginUserVo.setAccount(user.getAccount());
            return loginUserVo;
        }

        return null;
    }
}
